package io.kubesphere.devops;

import io.kubesphere.devops.core.*;
import io.kubesphere.devops.service.MergeLogService;
import io.kubesphere.devops.service.SimpleLogListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    /**
     * 初始化执行监听器,将监听器加入listener容器
     *
     * @param service
     * @return
     */
    @Bean
    SimpleLogListener simpleLogListener(MergeLogService service) {
        SimpleLogListener logListener = new SimpleLogListener(service);
        ListenerHolder.getInstance().setSimpleLogListener(logListener);
        MergingEventCenter.register(logListener);
        return logListener;
    }

    @Bean
    public FilterRegistrationBean<CommonFilter> golfContextFilterRegistrationBean() {
        CommonFilter filter = new CommonFilter();
        FilterRegistrationBean<CommonFilter> filterRegistrationBean = new FilterRegistrationBean<>(filter);
        return filterRegistrationBean;
    }

    @Bean
    DatasourceConfigMap datasourceConfigMap(MultipleDataSourceProperties multipleDataSourceProperties) {
        DatasourceConfigMap map = new DatasourceConfigMap();
        DataSourceProperty zone1 = multipleDataSourceProperties.getDataSourceProperty("zone1");
        map.setPartitionZone(Integer.parseInt(zone1.getPartitionIndex()), PartitionZone.ZONE1);
        DataSourceProperty zone2 = multipleDataSourceProperties.getDataSourceProperty("zone2");
        map.setPartitionZone(Integer.parseInt(zone2.getPartitionIndex()), PartitionZone.ZONE2);
        return map;
    }
}
